package 标准接口;



import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;

import java.util.HashMap;
import java.util.Map;


public class CreateRequestinfo1690 {

//第一步做一次就行了  记录下来
    /**
     * 模拟缓存服务
     */
    private static final Map<String,String> SYSTEM_CACHE = new HashMap <>();

    /**
     * ecology 系统发放的授权许可证(appid)
     */
    private static final String APPID = "a677fb06-42f2-4672-af7e-c7bdf84a0079";


    public static void main(String[] args) {
        //2882 测试流程创建用这个id发起    需要测试流程的话workflowid用2082正式流程1690 换人创建要清一次cookie会有缓存
        HttpRequest.closeCookie();
        testRestful("http://oa.maxwell-gp.com.cn:8090","/api/workflow/paService/doCreateRequest","");
    }
    /**
     * 第一步：
     *
     * 调用 ecology 注册接口,根据 appid 进行注册,将返回服务端公钥和 Secret 信息
     * 好像是30分钟过期
     */

    public static Map<String,Object> testGetoken(String address){
        // 从系统缓存或者数据库中获取 ECOLOGY 系统公钥和 Secret 信息

        String secret ="7e34ac04-66e4-4be9-ac3c-89c025367e07";
        String spk = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC275dG8DgkX+1ORNNYZMsNnXSfo5FSQxVe5t/zx5FLSDqZAfUFW/HP+9QzF/Wjj30r+3N9uX3uSY1IpAaAZ4YfKpkJ2I6/2TzA2AJpV9eVfbOB6wRvhaDia8eJILNK3SF6sHtBr94z+0Y5jnm76jPc5wApqKNhoQTgD2fXFnQ0ywIDAQAB";

        SYSTEM_CACHE.put("SERVER_PUBLIC_KEY",spk);
        //ECOLOGY 返回的系统密钥
        SYSTEM_CACHE.put("SERVER_SECRET",secret);

        RSA rsa = new RSA(null,spk);
        //对秘钥进行加密传输，防止篡改数据
        String encryptSecret = rsa.encryptBase64(secret,CharsetUtil.CHARSET_UTF_8,KeyType.PublicKey);
        //调用 ECOLOGY 系统接口进行注册
        String data = HttpRequest.post(address+ "/api/ec/dev/auth/applytoken")
                .header("appid",APPID)
                .header("secret",encryptSecret)
                .header("time","3600")
                .execute().body();
        System.out.println("testGetoken()："+data);
        Map<String,Object> datas = JSONUtil.parseObj(data);
        //ECOLOGY 返回的 token
        // TODO 为 Token 缓存设置过期时间
        SYSTEM_CACHE.put("SERVER_TOKEN",StrUtil.nullToEmpty((String)datas.get("token")));
        return datas;
    }

    public static String testRestful(String address,String api,String jsonParams){
        //ECOLOGY 返回的 token
        String token=(String) testGetoken(address).get("token");
        String spk = SYSTEM_CACHE.get("SERVER_PUBLIC_KEY");

        //封装请求头参数
        RSA rsa = new RSA(null,spk);
        //对用户信息进行加密传输,暂仅支持传输 OA 用户 ID
        String encryptUserid = rsa.encryptBase64("2882",CharsetUtil.CHARSET_UTF_8,KeyType.PublicKey);
        //调用 ECOLOGY 系统接口
        String data = HttpRequest.post(address + api)
                .header("appid",APPID)
                .header("token",token)
                .header("userid",encryptUserid)
                //后面带//的就是要改的 其他的固定值
                .body(
                        "workflowId=2082&requestName=测试马上删333&"+
                                "mainData=["
                                + "{'fieldName':'bdbh','fieldValue':'QR-QA-24'},"
                                + "{'fieldName':'bbzt','fieldValue':'A/2'},"
                                + "{'fieldName':'guishu','fieldValue':'1'},"


                                + "{'fieldName':'sxrq','fieldValue':'2024/11/11'},"//开单日期
                                + "{'fieldName':'fxrq','fieldValue':'2024-11-11'},"//发现日期
                                + "{'fieldName':'FXSJ','fieldValue':'10:00'},"//发现时间

                                + "{'fieldName':'TBR','fieldValue':'2882'},"//提报人
                                + "{'fieldName':'SJZG','fieldValue':'243'},"//上级主管*

                                + "{'fieldName':'KDBM','fieldValue':'0'},"//开单部门*
                                + "{'fieldName':'ycly','fieldValue':'0'},"//异常来源*
                                + "{'fieldName':'FXGX','fieldValue':'66'},"//发现工序*

                                + "{'fieldName':'jxxz','fieldValue':'MX-SSG3A'},"//机型*
                                + "{'fieldName':'jx','fieldValue':'MX-SSG3A'},"//机型文本再传一遍*

                                + "{'fieldName':'pms_xmh','fieldValue':'d3415193-01e1-4030-b72b-e129a09174fe'},"//PMS项目号
                                + "{'fieldName':'th','fieldValue':'图号/规格'},"//图号/规格
                                + "{'fieldName':'kh','fieldValue':'客户'},"//客户
                                + "{'fieldName':'pd','fieldValue':'批单/单号'},"//批单/单号
                                + "{'fieldName':'pm','fieldValue':'品名'},"//品名
                                + "{'fieldName':'ph','fieldValue':'品号'},"//品号
                                + "{'fieldName':'zs','fieldValue':'333'},"//总数
                                + "{'fieldName':'cjs','fieldValue':'222'},"//抽检数
                                + "{'fieldName':'bls','fieldValue':'111'},"//不良数
                                + "{'fieldName':'blms','fieldValue':'不良描述'},"//不良描述


                                + "{'fieldName':'SCTP','fieldValue':[" +
                                "{'fileName':'微信图片1.jpg','filePath':'https://plt.maxwell-gp.com.cn:8056/galaxy/maxwell-review-server/review/api/file/previewImg?filePath=mwgroup/M00/00/5E/wKgAg2bVYAKADO1jAAKeR6317xo487.jpg'}," +
                                "{'fileName':'微信图片2.jpg','filePath':'https://plt.maxwell-gp.com.cn:8056/galaxy/maxwell-review-server/review/api/file/previewImg?filePath=mwgroup/M00/00/5E/wKgAg2bVYAKADO1jAAKeR6317xo487.jpg'}" +

                                "]},"//上传图片
                                + "]"

                )

                .execute().body();

        System.out.println("token:"+token);
        System.out.println("userid:"+encryptUserid);
        System.out.println("testRestful()："+data);

        return data;
    }



}
